<template>
  <el-card>
    <el-table :data="tableData1" style="width: 100%">
      <el-table-column
        :width="item.width"
        :prop="item.prop"
        :label="item.label"
        v-for="item in options"
        :key="item.rid"
      >
        <template v-slot="{ row }" v-if="item.prop === 'state'">
          <el-tag
            type="success"
            class="mx-1"
            effect="dark"
            round
            v-if="row.state === '1'"
          >
            已完成
          </el-tag>
          <el-tag
            type="warning"
            class="mx-1"
            effect="dark"
            round
            v-if="row.state === '2'"
          >
            待完成
          </el-tag>
          <el-tag
            type="danger"
            class="mx-1"
            effect="dark"
            round
            v-if="row.state === '0'"
          >
            已取消
          </el-tag>
        </template>
        <template #default="{ row }" v-else-if="item.prop === 'action'">
          <template v-if="row.state === '2'">
            <el-button
              type="primary"
              size="small"
              @click="handleDialogValue(row)"
              >完成</el-button
            >
            <el-button type="danger" size="small" @click="delUser(row)"
              >取消</el-button
            >
          </template>
          <el-button
            v-if="row.state === '1' || row.state === '0'"
            type="warning"
            size="small"
            @click="reset(row)"
            >撤销</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="currentPage4"
      v-model:page-size="queryForm.pagesize"
      :page-sizes="[2, 4, 6, 8]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  <Dialog
    v-model="dialogVisible"
    :dialogTitle="dialogTitle"
    v-if="dialogVisible"
    @initUserList="initGetUsers"
    :dialogTableValue="dialogTableValue"
  />
</template>

<script setup>
import { ref } from 'vue'
import { options } from './options'
import { getRecode, editRecode } from '@/api/recode'
import Dialog from './components/dialog.vue'
const queryForm = ref({
  query: '',
  pagenum: 1,
  pagesize: 2
})
const dialogTitle = ref('')
const dialogVisible = ref(false)
const dialogTableValue = ref({})

const tableData1 = ref([])

const display = () => {
  tableData1.value = []
  const a = (queryForm.value.pagenum - 1) * queryForm.value.pagesize
  for (var i = 0; i < total.value; i++) {
    if (i >= a && i < a + queryForm.value.pagesize) {
      console.log(tableData.value[i])
      tableData1.value.push(tableData.value[i])
    }
  }
}

const total = ref(0)
const tableData = ref([])
const initGetUsers = async () => {
  const res = await getRecode()
  tableData.value = res.data.datas
  total.value = res.data.total
  display()
}
initGetUsers()

const handleSizeChange = (pageSize) => {
  queryForm.value.pagenum = 1
  queryForm.value.pagesize = pageSize
  initGetUsers()
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pagenum = pageNum
  initGetUsers()
}

const handleDialogValue = async (row) => {
  const label = { rid: row.rid, state: '1' }
  await editRecode(label)
  initGetUsers()
}

const delUser = async (row) => {
  const label = { rid: row.rid, state: '0' }
  await editRecode(label)
  initGetUsers()
}

const reset = async (row) => {
  const label = { rid: row.rid, state: '2' }
  await editRecode(label)
  initGetUsers()
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
</style>